package di.ass.view;

import java.util.ArrayList;

import di.ass.database.DatabaseHandler;
import di.ass.database.Food;
import di.ass.input.AlertsAdd;
import di.ass.main.ShoppingListActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import di.ass.R;

public class ListFood extends Activity implements OnClickListener {

	private ArrayList<Food> arrayListFood;
	private ListFoodAdapter listFoodAdapter;
	private TextView lableListFood;
	private TextView statisticTextView, header;
	private Button add, del;
	private Button checkAllFood;
	private ImageButton back;
	private ImageButton home;
	private Button filterAll, filterDone, filterNotDone;
	int idFormula;
	DatabaseHandler db;
	private boolean state_checkAllFood = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list_food);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.food_title);
		lableListFood = (TextView) findViewById(R.id.foodOfDate);
		header = (TextView) findViewById(R.id.title_food);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		createAndViewDatabase(0);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		finish();
	}

	public boolean checkTick() {
		int i = 0;
		while (arrayListFood.size() > i && arrayListFood.size() > 0) {
			if (arrayListFood.get(i).getChecked())
				return true;
			i++;
		}
		return false;
	}

	public void deleteChecked() {
		DatabaseHandler db = new DatabaseHandler(this);
		int i = 0;
		if (arrayListFood.size() <= 0)
			return;
		while (arrayListFood.size() > i && arrayListFood.size() > 0) {
			if (arrayListFood.get(i).getChecked()) {
				db.deleteFood(arrayListFood.get(i));
			}
			i++;
		}
		createAndViewDatabase(0);
	}

	public void unCheck() {
		int i = 0;
		if (arrayListFood.size() <= 0)
			return;
		while (arrayListFood.size() > i && arrayListFood.size() > 0) {
			if (arrayListFood.get(i).getChecked()) {
				arrayListFood.get(i).setChecked(false);
			}
			i++;
		}
		createAndViewDatabase(0);
	}

	private void createAndViewDatabase(int statistic) {
		// TODO Auto-generated method stub
		add = (Button) findViewById(R.id.add_food);
		del = (Button) findViewById(R.id.del_food);
		back = (ImageButton) findViewById(R.id.idBack);
		home = (ImageButton) findViewById(R.id.idHome);
		statisticTextView = (TextView) findViewById(R.id.statistic);
		filterAll = (Button) findViewById(R.id.filterFoodAll);
		filterDone = (Button) findViewById(R.id.filterFoodDone);
		filterNotDone = (Button) findViewById(R.id.filterFoodNotDone);
		checkAllFood = (Button) findViewById(R.id.idCheckFoodAll);
		add.setOnClickListener(this);
		del.setOnClickListener(this);
		back.setOnClickListener(this);
		home.setOnClickListener(this);
		filterAll.setOnClickListener(this);
		filterDone.setOnClickListener(this);
		filterNotDone.setOnClickListener(this);
		Intent intentListFood = getIntent();
		Bundle myBundleFood = intentListFood.getExtras();
		idFormula = myBundleFood.getInt("idFormula");
		String dateFormula = myBundleFood.getString("dateFormula");
		String[] date;
		date = dateFormula.split("/");
		String nameFormula = myBundleFood.getString("nameFormula");
		arrayListFood = new ArrayList<Food>();
		listFoodAdapter = new ListFoodAdapter(this, R.layout.element_list_food,
				arrayListFood);
		final ListView listViewFood = (ListView) findViewById(R.id.listFood);
		listViewFood.setAdapter(listFoodAdapter);
		db = new DatabaseHandler(this);

		String[] lable_list_foods = null;
		String[] textButton = null;
		String[] title = null;
		String strDate = null;
		if (db.getLanguageUse().equals("Việt Nam")) {
			title = getResources().getStringArray(R.array.title_vi);
			lable_list_foods = getResources().getStringArray(
					R.array.lable_list_foods_vi);
			textButton = getResources().getStringArray(R.array.button_vi);
			strDate = date[2] + "/" + date[1] + "/" + date[0];
		} else if (db.getLanguageUse().equals("English")) {
			title = getResources().getStringArray(R.array.title_eng);
			textButton = getResources().getStringArray(R.array.button_eng);
			lable_list_foods = getResources().getStringArray(
					R.array.lable_list_foods_eng);
			strDate = date[1] + "/" + date[2] + "/" + date[0];
		}
		header.setText(title[2]);
		add.setText(textButton[0]);
		del.setText(textButton[1]);
		checkAllFood.setText(textButton[2]);
		filterAll.setText(textButton[3]);
		filterDone.setText(textButton[6]);
		filterNotDone.setText(textButton[7]);

		lableListFood.setText(lable_list_foods[1] + ": " + nameFormula + "\n"
				+ lable_list_foods[3] + ": " + strDate);
		lableListFood.setGravity(Gravity.CENTER);
		final Food[] foods = db.getFood(idFormula, statistic);
		if (foods == null)
			statisticTextView.setText(lable_list_foods[2] + ": 0");
		else
			statisticTextView
					.setText(lable_list_foods[2] + ": " + foods.length);

		statisticTextView.setGravity(Gravity.CENTER);

		if (!state_checkAllFood && listViewFood.getCount() > 0) {
			checkAllFood.setBackgroundResource(R.drawable.check_all);
			state_checkAllFood = true;
		} else {
			checkAllFood.setBackgroundResource(R.drawable.uncheck_all);
			state_checkAllFood = false;
		}
		final Animation animation2 = AnimationUtils.loadAnimation(this,
				R.anim.btn_style);
		checkAllFood.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				checkAllFood.startAnimation(animation2);
				if (!state_checkAllFood && listViewFood.getCount() > 0) {
					checkAllFood.setBackgroundResource(R.drawable.check_all);
					state_checkAllFood = true;
					int count = listViewFood.getCount();
					for (int i = 0; i < count; i++) {
						ViewGroup row = (ViewGroup) listViewFood.getChildAt(i);
						CheckBox check = (CheckBox) row
								.findViewById(R.id.idCheckFood);
						check.setChecked(!foods[i].getChecked());
					}
				} else {
					checkAllFood.setBackgroundResource(R.drawable.uncheck_all);
					state_checkAllFood = false;
					int count = listViewFood.getCount();
					for (int i = 0; i < count; i++) {
						ViewGroup row = (ViewGroup) listViewFood.getChildAt(i);
						CheckBox check = (CheckBox) row
								.findViewById(R.id.idCheckFood);
						check.setChecked(false);
					}
				}
			}
		});

		if (foods != null)
			for (Food f : foods) {
				Food food = new Food(f.getId(), f.getId_formula(), f.getName(),
						f.getQuantity(), f.getUnitary(), f.getChecked());
				arrayListFood.add(food);
				listFoodAdapter.notifyDataSetChanged();
			}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.btn_style);
		switch (v.getId()) {
		case R.id.idHome:
			home.startAnimation(animation);
			Context context = v.getContext();
			Intent intent = new Intent(context, ShoppingListActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			context.startActivity(intent);
			break;
		case R.id.idBack:
			back.startAnimation(animation);
			onDestroy();
			break;
		case R.id.add_food:
			add.startAnimation(animation);
			AlertDialog alertDialog = AlertsAdd.ShowAddFoodDialog(this,
					idFormula);
			alertDialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					state_checkAllFood = true;
					createAndViewDatabase(0);
					listFoodAdapter.notifyDataSetChanged();
				}
			});
			alertDialog.show();
			break;
		case R.id.del_food:
			del.startAnimation(animation);
			if (checkTick()) {
				String[] textDel = null;
				if (db.getLanguageUse().equals("Việt Nam"))
					textDel = getResources().getStringArray(
							R.array.delete_food_vi);
				else if (db.getLanguageUse().equals("English")) {
					textDel = getResources().getStringArray(
							R.array.delete_food_eng);

				}

				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(textDel[0]);
				builder.setMessage(textDel[1]);
				builder.setPositiveButton(textDel[2],
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								deleteChecked();
							}
						});
				builder.setNeutralButton(textDel[3],
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								unCheck();
							}
						});
				builder.setIcon(R.drawable.info_warn);
				builder.show();
			}
			break;
		case R.id.filterFoodAll:
			filterAll.startAnimation(animation);
			createAndViewDatabase(0);
			break;
		case R.id.filterFoodDone:
			filterDone.startAnimation(animation);
			createAndViewDatabase(1);
			break;
		case R.id.filterFoodNotDone:
			filterNotDone.startAnimation(animation);
			createAndViewDatabase(-1);
			break;
		}
	}
}
